<template>
  <div class="login">
    <!-- 头部Logo区域 -->
    <div class="login-header">
      <div class="logo-container">
        <img src="/images/logoText.png" alt="智碳未来" class="logo-text" />
      </div>
    </div>

    <!-- 主要内容区域 -->
    <div class="login-content">
      <!-- 左侧插画区域 -->
      <div class="login-left">
        <div class="illustration-container">
          <img src="/images/bg_01.png" alt="智碳未来插画" class="main-illustration" />
          <div class="illustration-text">
            <h1 class="main-title">智碳未来·能源管理系统</h1>
            <p class="sub-title">我为地球降一度</p>
          </div>
        </div>
      </div>

      <!-- 右侧登录表单区域 -->
      <div class="login-right">
        <div class="login-form-container">
          <h3 class="form-title">账号登录</h3>
          <el-form
            ref="loginRef"
            :model="loginForm"
            :rules="loginRules"
            class="login-form"
          >
            <el-form-item prop="username">
              <el-input
                v-model="loginForm.username"
                type="text"
                size="large"
                auto-complete="off"
                placeholder="请输入账号"
              />
            </el-form-item>
            <el-form-item prop="password">
              <el-input
                v-model="loginForm.password"
                :type="passwordVisible ? 'text' : 'password'"
                size="large"
                auto-complete="off"
                placeholder="请输入密码"
                @keyup.enter="handleLogin"
              >
                <template #suffix>
                  <svg-icon 
                    :icon-class="passwordVisible ? 'eye-open' : 'eye'" 
                    class="password-toggle" 
                    @click="togglePasswordVisibility"
                  />
                </template>
              </el-input>
            </el-form-item>
            <el-form-item prop="code" v-if="captchaEnabled">
              <div class="captcha-container">
                <el-input
                  v-model="loginForm.code"
                  size="large"
                  auto-complete="off"
                  placeholder="请输入验证码"
                  @keyup.enter="handleLogin"
                />
                <div class="captcha-image">
                  <img :src="codeUrl" @click="getCode" class="captcha-img" />
                </div>
              </div>
            </el-form-item>
            <el-checkbox
              v-model="loginForm.rememberMe"
              class="remember-checkbox"
            >
              记住密码
            </el-checkbox>
            <el-form-item class="login-button-item">
              <el-button
                :loading="loading"
                size="large"
                class="login-button"
                @click.prevent="handleLogin"
              >
                <span v-if="!loading">登录</span>
                <span v-else>登录中...</span>
              </el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>

    <!-- 底部版权信息 -->
    <div class="login-footer">
      <span class="copyright-text">
         © 2017-2025 zhitancloud.com All rights reserved.
      </span>
    </div>
  </div>
</template>

<script setup>
import { getCodeImg } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from "@/utils/jsencrypt";
import useUserStore from "@/store/modules/user";

const userStore = useUserStore();
const route = useRoute();
const router = useRouter();
const { proxy } = getCurrentInstance();

const loginForm = ref({
  username: "admin",
  password: "888888",
  rememberMe: false,
  code: "",
  uuid: "",
});

const loginRules = {
  username: [{ required: true, trigger: "blur", message: "请输入您的账号" }],
  password: [{ required: true, trigger: "blur", message: "请输入您的密码" }],
  code: [{ required: true, trigger: "change", message: "请输入验证码" }],
};

const codeUrl = ref("");
const loading = ref(false);
// 验证码开关
const captchaEnabled = ref(true);
// 注册开关
const register = ref(false);
const redirect = ref(undefined);
// 密码可见性
const passwordVisible = ref(false);

watch(
  route,
  (newRoute) => {
    redirect.value = newRoute.query && newRoute.query.redirect;
  },
  { immediate: true }
);

function handleLogin() {
  proxy.$refs.loginRef.validate((valid) => {
    if (valid) {
      loading.value = true;
      // 勾选了需要记住密码设置在 cookie 中设置记住用户名和密码
      if (loginForm.value.rememberMe) {
        Cookies.set("username", loginForm.value.username, { expires: 30 });
        Cookies.set("password", encrypt(loginForm.value.password), {
          expires: 30,
        });
        Cookies.set("rememberMe", loginForm.value.rememberMe, { expires: 30 });
      } else {
        // 否则移除
        Cookies.remove("username");
        Cookies.remove("password");
        Cookies.remove("rememberMe");
      }
      // 调用action的登录方法
      userStore
        .login(loginForm.value)
        .then(() => {
          const query = route.query;
          const otherQueryParams = Object.keys(query).reduce((acc, cur) => {
            if (cur !== "redirect") {
              acc[cur] = query[cur];
            }
            return acc;
          }, {});
          router.push({ path: redirect.value || "/", query: otherQueryParams });
        })
        .catch(() => {
          loading.value = false;
          // 重新获取验证码
          if (captchaEnabled.value) {
            getCode();
          }
        });
    }
  });
}

function getCode() {
  getCodeImg().then((res) => {
    captchaEnabled.value =
      res.captchaEnabled === undefined ? true : res.captchaEnabled;
    if (captchaEnabled.value) {
      codeUrl.value = "data:image/gif;base64," + res.img;
      loginForm.value.uuid = res.uuid;
    }
  });
}

function togglePasswordVisibility() {
  passwordVisible.value = !passwordVisible.value;
}

function getCookie() {
  const username = Cookies.get("username");
  const password = Cookies.get("password");
  const rememberMe = Cookies.get("rememberMe");
  loginForm.value = {
    username: username === undefined ? loginForm.value.username : username,
    password:
      password === undefined ? loginForm.value.password : decrypt(password),
    rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
  };
}

getCode();
getCookie();
</script>

<style lang="scss" scoped>
// 引入自定义字体
@font-face {
  font-family: 'SourceHanSansCN-Medium';
  src: url('@/assets/ttf/SourceHanSansCN-Medium.otf') format('opentype');
  font-weight: normal;
  font-style: normal;
}

.login {
  min-height: 100vh;
  width: 100%;
  background: #f5f5f5;
  background-image: url('/images/bg_02.png');
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  position: relative;
  display: flex;
  flex-direction: column;
}

// 头部Logo区域
.login-header {
  position: absolute;
  top: 20px;
  left: 20px;
  z-index: 10;
  
  .logo-container {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    
    .logo-text {
      height: 66px;
      width: 200px;
    }
    
    .logo-subtitle {
      font-size: 12px;
      color: #666;
      margin-top: 4px;
      font-family: 'SourceHanSansCN-Medium', sans-serif;
    }
  }
}

// 主要内容区域
.login-content {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 80px 20px 20px 20px;
  gap: 150px;
}

// 左侧插画区域
.login-left {
  flex: 1;
  max-width: 600px;
  position: relative;
  
  .illustration-container {
    position: relative;
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    
    .main-illustration {
      width: 700px;
      height: 445px;
    }
    
    .illustration-text {
      position: absolute;
      top: -40px;
      left: 5px;
      z-index: 5;
      
      .main-title {
        font-size: 28px;
        font-weight: normal;
        color: #000;
        margin: 0 0 12px 0;
        font-family: 'SourceHanSansCN-Medium', sans-serif;
        line-height: 1.2;
        text-align: left;
      }
      
      .sub-title {
        font-size: 40px;
        font-weight: bold;
        color: #000;
        margin: 0;
        font-family: 'SourceHanSansCN-Medium', sans-serif;
        line-height: 1.2;
        text-align: center;
        width: 100%;
      }
    }
  }
}

// 右侧登录表单区域
.login-right {
  flex: 0 0 500px;
  
  .login-form-container {
    background: #fff;
    border-radius: 12px;
    padding: 40px;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
    
    .form-title {
      font-size: 24px;
      font-weight: 600;
      color: #333;
      margin: 0 0 32px 0;
      text-align: center;
      position: relative;
      font-family: 'SourceHanSansCN-Medium', sans-serif;
      
      &::after {
        content: '';
        position: absolute;
        bottom: -8px;
        left: 50%;
        transform: translateX(-50%);
        width: 40px;
        height: 2px;
        background: linear-gradient(135deg, #1EB897, #49BD3F);
        border-radius: 1px;
      }
    }
    
    .login-form {
      .el-form-item {
        margin-bottom: 24px;
        
        .el-input {
          height: 48px;
          
          :deep(.el-input__wrapper) {
            border-radius: 8px;
            border: 1px solid #e0e0e0;
            box-shadow: none;
            
            &:hover {
              border-color: #1EB897;
            }
            
            &.is-focus {
              border-color: #1EB897;
              box-shadow: 0 0 0 2px rgba(30, 184, 151, 0.1);
            }
          }
          
          :deep(.el-input__inner) {
            font-size: 14px;
            color: #333;
            height: 46px;
            line-height: 46px;
          }
          
          .password-toggle {
            cursor: pointer;
            color: #999;
            transition: color 0.3s ease;
            
            &:hover {
              color: #1EB897;
            }
          }
        }
      }
      
      .captcha-container {
        display: flex;
        gap: 12px;
        
        .el-input {
          flex: 1;
        }
        
        .captcha-image {
          flex: 0 0 120px;
          
          .captcha-img {
            width: 100%;
            height: 48px;
            border-radius: 8px;
            border: 1px solid #e0e0e0;
            cursor: pointer;
            object-fit: cover;
          }
        }
      }
      
      .remember-checkbox {
        margin-bottom: 24px;
        
        :deep(.el-checkbox__label) {
          font-size: 14px;
          color: #666;
        }
      }
      
      .login-button-item {
        margin-bottom: 0;
        
        .login-button {
          width: 100%;
          height: 48px;
          background: linear-gradient(135deg, #1EB897, #49BD3F);
          border: none;
          border-radius: 8px;
          font-size: 16px;
          font-weight: 600;
          color: #fff;
          font-family: 'SourceHanSansCN-Medium', sans-serif;
          transition: all 0.3s ease;
          
          &:hover {
            background: linear-gradient(135deg, #1a9d82, #3fa835);
            transform: translateY(-1px);
            box-shadow: 0 4px 16px rgba(30, 184, 151, 0.3);
          }
          
          &:active {
            transform: translateY(0);
          }
          
          &.is-loading {
            background: linear-gradient(135deg, #1EB897, #49BD3F);
          }
        }
      }
    }
  }
}

// 底部版权信息
.login-footer {
  position: absolute;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  
  .copyright-text {
    font-size: 12px;
    color: #999;
    font-family: 'SourceHanSansCN-Medium', sans-serif;
  }
}

// 响应式设计
@media (max-width: 1200px) {
  .login-content {
    gap: 100px;
  }
  
  .login-left {
    max-width: 500px;
    
    .illustration-text {
      .main-title {
        font-size: 24px;
      }
      
      .sub-title {
        font-size: 32px;
      }
    }
  }
  
  .login-right {
    flex: 0 0 350px;
  }
}

@media (max-width: 768px) {
  .login-content {
    flex-direction: column;
    gap: 30px;
    padding: 60px 15px 15px 15px;
  }
  
  .login-left {
    max-width: 100%;
    order: 2;
    
    .illustration-text {
      .main-title {
        font-size: 20px;
      }
      
      .sub-title {
        font-size: 28px;
      }
    }
  }
  
  .login-right {
    flex: none;
    width: 100%;
    max-width: 400px;
    order: 1;
    
    .login-form-container {
      padding: 30px 20px;
    }
  }
  
  .login-header {
    top: 15px;
    left: 15px;
    
    .logo-container .logo-text {
      height: 32px;
    }
  }
}

@media (max-width: 480px) {
  .login-left .illustration-text {
    .main-title {
      font-size: 18px;
    }
    
    .sub-title {
      font-size: 24px;
    }
  }
  
  .login-right .login-form-container {
    padding: 25px 15px;
    
    .form-title {
      font-size: 20px;
    }
  }
}
</style>
